package com.itls.recommendedgames.control;

import com.itls.recommendedgames.entity.UserLibrary;
import com.itls.recommendedgames.entity.UserLibraryGameDTO;
import com.itls.recommendedgames.service.UserLibraryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/library")
@CrossOrigin
@Tag(name="库存管理")
public class UserLibraryControl {
    @Resource
    private UserLibraryService userLibraryService;

    @PostMapping("/addUserLibrary")
    @Operation(summary = "添加入库")
    public boolean addToLibrary(@RequestBody UserLibrary userLibrary) {
     return userLibraryService.addToLibrary(userLibrary);
    }

    @PostMapping("/selectUserLibrary")
    @Operation(summary = "筛选入库游戏")
    public  List<Integer> getUserLibraryIds(@RequestBody UserLibrary userLibrary) {
        return userLibraryService.getUserGameIds(userLibrary);

    }

    @PostMapping("/updatePlayTime")
    @Operation(summary = "更新游玩时间与最近游玩时间")
    public boolean updatePlayTime(@RequestBody UserLibrary userLibrary) {
        if (userLibrary.getUserId() == null || userLibrary.getGameId() == null) {
            return false;
        }
        // play_time_hours 是本次增加的时长（小时），不是累计值
        return userLibraryService.updatePlayTime(
                userLibrary.getUserId(),
                userLibrary.getGameId(),
                userLibrary.getPlayTimeHours()
        );
    }

    @GetMapping("/getLibrary/{userId}")
    @Operation(summary = "获取库中游戏信息 - 路径参数")
    public List<UserLibraryGameDTO> getUserLibrary(@PathVariable Integer userId) {
        return userLibraryService.getUserLibrary(userId);
    }

    @GetMapping(value = "/getLibrary", params = "userId")
    @Operation(summary = "获取库中游戏信息 - 查询参数")
    public List<UserLibraryGameDTO> getUserLibraryByQuery(@RequestParam Integer userId) {
        return userLibraryService.getUserLibrary(userId);
    }

    @DeleteMapping("/removeGame")
    @Operation(summary = "从用户游戏库中删除游戏")
    public boolean removeGame(@RequestParam Integer userId, @RequestParam Integer gameId) {
        if (userId == null || gameId == null) {
            return false;
        }
        return userLibraryService.removeGame(userId, gameId);
    }

    @DeleteMapping("/removeGame/{userId}/{gameId}")
    @Operation(summary = "从用户游戏库中删除游戏 - 路径参数")
    public boolean removeGameByPath(@PathVariable Integer userId, @PathVariable Integer gameId) {
        if (userId == null || gameId == null) {
            return false;
        }
        return userLibraryService.removeGame(userId, gameId);
    }

}
